package com.littlerpc.registry.zookeeper;

import java.util.Random;

/**
 * Created by ChengPeng on 2017/7/5.
 */
public class TestZk {

    public static void main(String[] args) {
        ZookeeperConfig conf = new ZookeeperConfig();
        conf.setConnectString("localhost:2181");
        conf.setTimeout(100000);

        ZookeeperRegistry registry = new ZookeeperRegistry(conf);

        String ip_formatter = "192.168.1.%d";
        Random r = new Random();

        new Thread(() -> {
            for(int i =0;i<10;i++){
                String ip = String.format(ip_formatter,i);

                if(registry.isConnected()){
                     registry.register(ip);
                    System.out.println("registry*******:"+ ip);
                    try {
                        Thread.sleep(r.nextInt(5000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "registry").start();

        ZookeeperDiscovery discovery = new ZookeeperDiscovery(conf);

        new Thread(() -> {
          while (true && discovery.isConnected()) {
              try {
                  Thread.sleep(r.nextInt(5000));
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }

              System.out.println(discovery.getProviderListCache());
          }
        }, "discover").start();

        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
